package com.second.ssyt_test.common.util;

import java.util.List;

import org.apache.commons.lang3.Validate;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.second.ssyt_test.common.Constant;

/**

 * Alibaba的Fastjson,如果为JAVA Bean,必须有无参构造和getter setter。

 * 

 * @author Runs。

 * @date 2013-12-2

 */
public class FastjisonUtil {

	private FastjisonUtil() {

	}

	public static <T> List<T> parseArray(String jsonString, Class<T> clazz) {
		parseValidate(jsonString, clazz);

		return JSON.parseArray(jsonString, clazz);
	}

	public static <T> T parseObject(String jsonString, Class<T> clazz) {
		parseValidate(jsonString, clazz);

		return JSON.parseObject(jsonString, clazz);
	}

	public static String toJson(Object obj) {
		return JSON.toJSONString(obj);
	}

	public static String toJsonWithDateFormat(Object obj, String dateFormat) {
		return JSON.toJSONStringWithDateFormat(obj, dateFormat,
				SerializerFeature.WriteDateUseDateFormat);
	}

	private static void parseValidate(String jsonString, Class<?> clazz) {
		Validate.notBlank(jsonString, "JSON串" + Constant.VALIDATE_NOT_BLANK);
		Validate.notNull(clazz, "Class" + Constant.VALIDATE_NOT_NULL);
	}
}
